package com.me.mygdxgame.models;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.me.mygdxgame.assets.Assets;

public class Player extends AbstractGameObject{
	
	public static String TAG = Player.class.getName();
	
	private TextureRegion regPlayer;
	private World b2dworld;
	private final float JUMP_TIME_MAX = 0.3f;
	private final float JUMP_TIME_MIN = 0.1f;
	private final float JUMP_TIME_OFFSET_FLYING = JUMP_TIME_MAX - 0.018f;
	
	public enum VIEW_DIRECTION{LEFT, RIGHT};
	
	public enum JUMP_STATE{
		GROUNDED, FALLING, JUMP_RISING, JUMP_FALLING
	}
	
	public VIEW_DIRECTION viewDirection;
	public float timeJumping;
	public JUMP_STATE jumpState;
	public boolean hasFeatherPowerup;
	public float timeLeftFeatherPowerup;
	
	public Player(World b2world){
		this.b2dworld = b2world;
		init();
	}

	private void init() {
//		dimension.set(49, 77);
//		regPlayer = Assets.player;
//		origin.set(dimension.x/2f, dimension.y/2f);
//		bounds.set(0, 0, dimension.x, dimension.y);
//		position.set(160, 160);
		sprite.setRegion(Assets.player);
		sprite.setPosition(160, 300);
		sprite.setSize(40, 70);
		sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
		sprite.setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
		body = BoxBuilder.CreateBoxBody(b2dworld, BodyType.DynamicBody, 160, 300, 
				sprite.getWidth(),sprite.getHeight(), new FixtureDef(), sprite, 0.5f, 0.4f, 0f);
		body.setFixedRotation(true);
		terminalVelocity.set(3, 4);
		friction.set(12, 0);
		acceleration.set(0, -25);
		viewDirection = VIEW_DIRECTION.RIGHT;
		jumpState = JUMP_STATE.GROUNDED;
		timeJumping = 0;
		hasFeatherPowerup = false;
		timeLeftFeatherPowerup = 0;
	}

	@Override
	public void update(float deltaTime) {
		super.update(deltaTime);
		if(velocity.x != 0){
			viewDirection = velocity.x < 0 ? VIEW_DIRECTION.LEFT : VIEW_DIRECTION.RIGHT;
		}
	}

	@Override
	protected void updateMotionY(float deltaTime) {
		switch (jumpState) {
			case GROUNDED:
				jumpState = JUMP_STATE.FALLING;
				break;
			case JUMP_RISING:
				timeJumping += deltaTime;
				if (timeJumping <= JUMP_TIME_MAX) {
					velocity.y = terminalVelocity.y;
				}
				break;
			case FALLING:
				break;
			case JUMP_FALLING:
				timeJumping += deltaTime;
				if (timeJumping > 0 && timeJumping <= JUMP_TIME_MIN) {
						velocity.y = terminalVelocity.y;
				}
		}
		if (jumpState != JUMP_STATE.GROUNDED)
		super.updateMotionY(deltaTime);
	}

	@Override
	public void render(SpriteBatch batch) {
		TextureRegion reg = null;
		reg = regPlayer;
//		System.out.println(body.getAngle());
//		rotation = 0.9f*MathUtils.radiansToDegrees;
//		origin.set(dimension.x/2f, dimension.y/2f);
//		batch.draw(reg.getTexture(),
//				position.x - dimension.x/2, position.y - dimension.y/2,
//				origin.x, origin.y,
//				dimension.x, dimension.y,
//				scale.x, scale.y,
//				rotation,
//				reg.getRegionX(), reg.getRegionY(),
//				reg.getRegionWidth(), reg.getRegionHeight(),
//				false, false);
		sprite.draw(batch);
//		batch.draw(Assets.player,body.getPosition().x*BoxBuilder.BOX_TO_WORLD, body.getPosition().y*BoxBuilder.BOX_TO_WORLD);
//		System.out.println(dimension.x);
		
	}
	
	public void setJumping(boolean jumpKeyPressed){
		switch (jumpState) {
		case GROUNDED:
			if(jumpKeyPressed){
				System.out.println("ok");
				body.setLinearVelocity(body.getLinearVelocity().x, 10);
				timeJumping = 0;
//				jumpState = JUMP_STATE.JUMP_RISING;
			}
			break;
		case JUMP_RISING:
			if(!jumpKeyPressed){
				jumpState = JUMP_STATE.JUMP_FALLING;
			}
			break;
		case FALLING:
		case JUMP_FALLING:
			if(jumpKeyPressed && hasFeatherPowerup){
				timeJumping = JUMP_TIME_OFFSET_FLYING;
				jumpState = JUMP_STATE.JUMP_RISING;
			}
			break;
		}
	}
	
	public void setFeatherPowerup(boolean pickedUp){
		
	}

}
